<?php namespace Phpcmf\Controllers\Member;

class Hdbm extends \Phpcmf\Table
{

    public $cid; // 内容id
    public $form; // 表单信息

    public function __construct() {
        parent::__construct();
        // 初始化模块
        $this->_module_init(APP_DIR);
        // 判断表单是否操作
        $this->form = $this->module['form'][\Phpcmf\Service::L('Router')->class];
        !$this->form && $this->_msg(0, dr_lang('模块表单【%s】不存在', \Phpcmf\Service::L('Router')->class));
        // 支持附表存储
        $this->is_data = 1;
        // 模板前缀(避免混淆)
        $this->tpl_prefix = '';
        // 单独模板命名
        $this->tpl_name = 'hdbm';
        // 模块显示名称
        $this->name = dr_lang('内容模块[%s]表单（%s）', APP_DIR, $this->form['name']);
        // 获取父级内容
        $this->cid = intval(\Phpcmf\Service::L('Input')->get('cid'));
        if ($this->cid) {
            $this->index = $this->content_model->get_row( $this->cid);
            //$this->index['uid'] != $this->uid && $this->_msg(0, dr_lang('无权限操作他人数据'));
        }
        // 自定义条件
        $where = 'cid='. $this->cid;
        $sysfield = ['displayorder'];
        // 初始化数据表
        $this->_init([
            'field' => $this->form['field'],
            'table' => dr_module_table_prefix(APP_DIR).'_form_'.$this->form['table'],
            'sys_field' => $sysfield,
            'date_field' => 'inputtime',
            'list_field' => $this->form['setting']['list_field'],
            'show_field' => 'title',
            'order_by' => 'inputtime desc',
            'where_list' => $where,
        ]);
        // 写入模板
        \Phpcmf\Service::V()->assign([
            'mform' => $this->form,
            'index' => $this->index,
            'field' => $this->form['field'],
        ]);
    }

    public function index() {

        $tid = dr_safe_filename($_GET['tid']);
		if ($this->index['uid'] != $this->uid) {
			$this->_msg(0, dr_lang('这个不是您的圈子，无法修改分类'));
		}
        if (strlen($tid)) {
            $tid = intval($tid);
            $this->init['where_list'].= ' and tid='.$tid;
        }
        $this->init['order_by'] = 'displayorder asc,id asc';

        $p = [
            'cid' =>  $this->cid,
            'tid' =>  $tid,
        ];

        $this->_List($p);

        $stype = \Phpcmf\Service::M('content', 'quan')->son_type($this->index['son_type']);

        \Phpcmf\Service::V()->assign([
            'p' => $p,
            'stype' => $stype,
            'sname' => strlen($tid) ? $stype[$tid] : '全部',
        ]);
        \Phpcmf\Service::V()->display('son_list.html');
    }

    public function add() {
		//判断字节
		$day_posts = \Phpcmf\Service::M()->db->table('module_form')->where('module', MOD_DIR)->where('table', $this->form['name'])->get()->getRowArray();
		$day_post = dr_string2array($day_posts['setting']);
		if  (IS_AJAX) {
			$tid = \Phpcmf\Service::L('Input')->get('tid');
			//$datas = \Phpcmf\Service::L('Input')->post('data');
			//$date = \Phpcmf\Service::L('Input')->get('date');
			if(!$tid) {
				$this->_msg(0, 'ID参数不正确');
			}
			//获取
			$huodongid = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_form_son')->where('id', $tid)->get()->getRowArray();
			if(!$huodongid) {
				$this->_msg(0, 'ID参数不正确');
			}
			//if($yicanjia) {
			//标题
			if($huodongid['title']) {
				$titles = $huodongid['title'];
			} else {
				$titles = dr_strcut(dr_clearhtml($huodongid['neirong']), 30);
			}
			if(!$this->uid) {
				$this->_msg(0, '请先登陆');
			}
			$baomingtongji= \Phpcmf\Service::M()->db->table($this->init['table'])->where('tid', $tid)->countAllResults();
			if( $baomingtongji >= $huodongid['hdrs']) {
				$this->_msg(0, '人数已满了');
			}
			if($this->index['hdtc'] == 0) {
				$yicanjia= \Phpcmf\Service::M()->db->table($this->init['table'])->where('uid', $this->uid)->where('tid', $tid)->where('cid', $this->index['id'])->get()->getRowArray();
				
				\Phpcmf\Service::M()->table($this->init['table'])->where('uid', $this->uid)->where('tid', $tid)->where('cid', $this->index['id'])->delete();
				\Phpcmf\Service::M()->table($this->init['table'].'_data_0')->where('cid', $this->index['id'])->where('uid', $this->uid)->delete();
				if($yicanjia) {
					$this->_msg(1, '退出活动');
				}

			} else {
				$yicanjia= \Phpcmf\Service::M()->db->table($this->init['table'])->where('uid', $this->uid)->where('tid', $tid)->where('cid', $this->index['id'])->get()->getRowArray();
				if($yicanjia) {
					$this->_msg(1, '您已经报名了');
				}
			}
			
			//手机号码
			//$shoujihaoma = 
			//dr_debug('$this', $this->index);
			$ip = \Phpcmf\Service::L('Input')->ip_info();
		    \Phpcmf\Service::M()->db->table($this->init['table'])->insert(array(
		        'cid' => $this->index['id'],
				'catid' => (int)$this->index['catid'],
		        'uid' => $this->uid,
				'author' => '',
				'inputip' => $ip,
				'inputtime' => SYS_TIME,
				'updatetime' => '',
				'title' => $titles,
				'status' => 1,
		        'tableid' => '0',
		        'displayorder' => '0',
		    	'shoujihaoma' => 0,
				'weixinhao' => 0,
				'tupian' => '',
				'wenjian' => '',
				'feiyong' => 0,
				'jifen' => 0,
				'tid' => $tid,
				'shenhe' => 1,
				'bmzt' => 1,
				'fufeijifen' => 0,
		    ));
			\Phpcmf\Service::M()->db->table($this->init['table'].'_data_0')->insert(array(
			    
			    'cid' => $this->index['id'],
			    'catid' => (int)$this->index['catid'],
			    'uid' => $this->uid,
			));
			//list($tpl) = $this->_Post(0);
			
			//更新统计
			$rt = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR.'_form_hdbm')->where('tid', $tid)->countAllResults();
			\Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR.'_form_son')->where('id', $tid)->update([
			    'huodongshuliang' => $rt,
			]);
			\Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR)->where('id', $tid)->update([
			    'hdbm_total' => $rt,
			]);
			$this->_msg(1, '报名成功');
			
		}


        // 请求错误
        $this->_json(0, dr_lang('请求错误'));
		/*!$this->cid && $this->_msg(0, dr_lang('缺少cid参数'));
		
		
		
		//判断字节
		$day_posts = \Phpcmf\Service::M()->db->table('module_form')->where('module', MOD_DIR)->where('table', 'hdbm')->get()->getRowArray();
		$day_post = dr_string2array($day_posts['setting']);
		if (IS_AJAX_POST) {
		    
			//$datasc = intval(\Phpcmf\Service::L('Input')->post('csrf_test_name'));
			$datas = \Phpcmf\Service::L('Input')->post('data');
			if ($datas['tid'] === 0) {
			    $this->_msg(0, '请选择分类', '');
			}
			if ( !$datas['neirong']) {
				
				$this->_msg(0, '内容不能为空', '');
			}
			preg_match_all("/./us", dr_preg_html($datas['neirong']),$match);
			$shuliang = count($match[0]);
			$shaozijie_post = $day_post['shaozijie'];
			$duozijie_post = $day_post['duozijie'];
			if ($shuliang < $shaozijie_post) {
				$this->_msg(0, '不能少于'.$shuliang.'个文字', '');
			}
			if ($shuliang > $duozijie_post) {
				$this->_msg(0, '不能多于'.$shuliang.'个文字', '');
			}
		}*/
		
		
		// 是否有验证码
		/*$this->is_post_code = \Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'code', $this->member);
		list($tpl) = $this->_Post(0);
		
		\Phpcmf\Service::V()->assign([
		    'form' =>  dr_form_hidden(),
		    'is_post_code' => $this->is_post_code,
		]);
		\Phpcmf\Service::V()->display($tpl);*/

    }
	public function renzheng() {
		    //帖子id
		    $tid = \Phpcmf\Service::L('Input')->get('tid');
		    if(!$tid){
		        $this->_json(0, dr_lang('帖子ID不能为空'));
		    }
		    //圈贴id
		    $quanid = \Phpcmf\Service::L('Input')->get('quan');
		    if(!$quanid){
		        $this->_json(0, dr_lang('圈子ID不能为空'));
		    }
		    //圈子管理员uid
		    $quanuid = \Phpcmf\Service::L('Input')->get('quanuid');
		    if(!$quanid){
		        $this->_json(0, dr_lang('圈主用户UID不能为空'));
		    }
		    //帖子用户uid
		    $tieuid = \Phpcmf\Service::L('Input')->get('tieuid');
		    if(!$tieuid){
		        $this->_json(0, dr_lang('帖子用户UID不能为空'));
		    }
		    //用户uid
		    $uid = \Phpcmf\Service::L('Input')->get('uid');
		    if(!$uid){
		        $this->_json(0, dr_lang('用户ID不能为空'));
		    }
		    if(!$this->uid) {
				$this->_json(0, dr_lang('请先登录'));
			}
			$user=dr_member_info($this->uid);
			if($user['is_admin']) {
				
			} elseif($this->uid == $quanuid || $this->uid == $tieuid){
			    
			} else {
			    $this->_json(0, dr_lang('您无权操作'));
			}
			$renzhengbaomingyonghu = \Phpcmf\Service::M()->db->table('1_quan_form_hdbm')->where('uid', $uid)->where('tid', $tid)->where('shenhe', 1)->get()->getRowArray();
		    if(!$renzhengbaomingyonghu){
		        $this->_json(0, dr_lang('没有这个名单，或者名单审核中。'));
		    }
		    $son = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_form_son')->where('id', $tid)->get()->getRowArray();
			//标题
			if($son['title']) {
				$titles = $son['title'];
			} else {
				$titles = dr_strcut(dr_clearhtml($son['neirong']), 30);
			}
		    $ip = \Phpcmf\Service::L('Input')->ip_info();
			\Phpcmf\Service::M()->db->table('1_quan_form_hdbm_renzheng')->insert(array(
			        'cid' => $tid,
					'catid' => (int)$this->index['catid'],
			        'uid' => $this->uid,
			        'quanid' => $quanid,
					'author' => '',
					'inputip' => $ip,
					'inputtime' => SYS_TIME,
					'updatetime' => '',
					'title' => $titles,
					'status' => 1,
			        'tableid' => '0',
			        'displayorder' => '0',
			    	'shoujihaoma' => 0,
					'weixinhao' => 0,
					'feiyong' => 0,
					'jifen' => 0,
					'tid' => $tid,
					'shenhe' => 1,
					'bmzt' => 1,
					'fufeijifen' => 0,
			    ));
			$datas=1;
			$this->_json(1, dr_lang('认证成功'),$datas);
		}
    //提交报名资料
	public function addziliao() {
	
	    !$this->cid && $this->_msg(0, dr_lang('缺少cid参数'));

		
		//判断字节
		//获取
		$tid = \Phpcmf\Service::L('Input')->get('tid');
		$son = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_form_son')->where('id', $tid)->get()->getRowArray();
		//标题
		if($son['title']) {
			$titles = $son['title'];
		} else {
			$titles = dr_strcut(dr_clearhtml($son['neirong']), 30);
		}
		$baomingtongji= \Phpcmf\Service::M()->db->table($this->init['table'])->where('tid', $tid)->countAllResults();
		if( $baomingtongji >= $son['hdrs']) {
			$this->_msg(0, '人数已满了');
		}
		//dr_debug('$son', $son);
		$yicanjia= \Phpcmf\Service::M()->db->table($this->init['table'])->where('uid', $this->uid)->where('tid', $tid)->where('cid', $this->index['id'])->get()->getRowArray();
		if($yicanjia) {
			$this->_msg(1, '您已经报名了');
		}
		if (IS_AJAX_POST) {
			
		    
			//$datasc = intval(\Phpcmf\Service::L('Input')->post('csrf_test_name'));
			$datas = \Phpcmf\Service::L('Input')->post('data');
			if($son['hdsjh']==2) {
				if(!$datas['shoujihaoma']){
					$this->_msg(0, dr_lang('手机号不能为空'));
				}
			}
			if($son['hdwxh']==2) {
				if(!$datas['weixinhao']){
					$this->_msg(0, dr_lang('微信号不能为空'));
				}
			}
			if($son['sctp']==2) {
				if(!$datas['tupian']){
					$this->_msg(0, dr_lang('图片不能为空'));
				}
			}
			if($son['scfj']==2) {
				if(!$datas['wenjian']){
					$this->_msg(0, dr_lang('附件不能为空'));
				}
			}
			
			if ($son['hdfs'] == 4) {
				$inhuodongfufei = \Phpcmf\Service::M()->db->table('1_quan_huodong_buy')->where('tid', $tid)->where('uid', $this->uid)->get()->getRowArray();
				if ($inhuodongfufei) {
					
				} else {
					$this->_msg(0, dr_lang('请支付活动费用'));
				}
				//线上付费
				/*if ($this->member['money'] < $son['hdfy']) {
					$this->_msg(0, dr_lang('余额不足，请充值'));
				} else {
					//减去参与活动会员费用
					\Phpcmf\Service::M('pay')->add_money($this->uid, -$son['hdfy']);
					// 减去参与活动付款记录
					\Phpcmf\Service::M('pay')->add_paylog([
					    'uid' => $this->uid,
					    'username' => '',
					    'touid' => $son['uid'],
					    'tousername' => '',
					    'mid' => 'system',
					    'title' => '报名参与《'.$titles.'》消费',
					    'value' => -$son['hdfy'],
					    'type' => 'finecms',
					    'status' => 1,
					    'result' => '线上报名活动',
					    'paytime' => SYS_TIME,
					    'inputtime' => SYS_TIME,
					]);
					//平台收取费用分成
					$bicheng = \Phpcmf\Service::M()->table('1_quan_shezhi')->where('dirname', 'quan')->getRow();
					$bichengs = dr_string2array($bicheng['setting']);
					// 提成
					$bicheng = $bichengs['pages']['quan_huodong'];
					$shourubi = 1 - $bichengs['pages']['quan_huodong'];
					$shouru = $bichengs['pages']['quan_shouru'];
					if ($bicheng == 0) {
						$feiyong = abs($son['hdfy']);
						$shourubi = 0;
					} else {
						$feiyong = abs($son['hdfy']) * $bicheng;
						$shourubi = abs($son['hdfy']) * $shourubi;
					}
					
					dr_debug('feiyong', $shourubi.'+'.$bicheng);
					//$feiyong = abs($son['hdfy']) * $bicheng;
					//增加活动发起人收费
					\Phpcmf\Service::M('pay')->add_money($son['uid'], abs($feiyong));
					// 收款人增加一条收入记录
					\Phpcmf\Service::M('pay')->add_paylog([
					    'uid' => $son['uid'],
					    'username' => '',
					    'touid' => $this->uid,
					    'tousername' => '',
					    'mid' => 'system',
					    'title' => '报名参与《'.$titles.'》收入',
					    'value' => abs($feiyong),
					    'type' => 'finecms',
					    'status' => 1,
					    'result' => '线上报名活动',
					    'paytime' => SYS_TIME,
					    'inputtime' => SYS_TIME,
					]);
					// 总站收款人账户
					\Phpcmf\Service::M('pay')->add_money($shouru, $shourubi);
					// 总站收款人增加一条收入记录
					\Phpcmf\Service::M('pay')->add_paylog([
					    'uid' => $shouru,
					    'username' => '',
					    'touid' => $this->uid,
					    'tousername' => '',
					    'mid' => 'system',
					    'title' => '报名《'.$titles.'》收入',
					    'value' => $shourubi,
					    'type' => 'finecms',
					    'status' => 1,
					    'result' => '线上报名活动',
					    'paytime' => SYS_TIME,
					    'inputtime' => SYS_TIME,
					]);
					//$this->_msg(0, dr_lang('已经报名成功'));
				}*/
			} elseif ($son['hdfs'] == 5) {
				//线上积分
				if ($this->member['score'] < $son['huodongjifen']) {
					$this->_msg(0, dr_lang(SITE_SCORE.'不足，去做任务吧'));
				} else {
					//减去参与活动会员积分
					$url = '/index.php?s='.MOD_DIR.'&c=son&id='.$tid;
					\Phpcmf\Service::M('member')->add_score($this->uid, -$son['huodongjifen'], $note = '报名活动《'.$titles.'》'.SITE_SCORE.'消费', $url, $mark = '', $count = 10);
					// 增加发起活动积分收入记录
					\Phpcmf\Service::M('member')->add_score($son['uid'], $son['huodongjifen'], $note = '报名活动《'.$titles.'》'.SITE_SCORE.'收入', $url, $mark = '', $count = 10);
				}
			}
			
		}
	
	
	    // 是否有验证码
	    $this->is_post_code = \Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'code', $this->member);
	    list($tpl) = $this->_Post(0);
	
	    \Phpcmf\Service::V()->assign([
	        'form' =>  dr_form_hidden(),
	        'is_post_code' => $this->is_post_code,
	    ]);
	    \Phpcmf\Service::V()->display($tpl);
	
	}
	public function shenqing() {
		$datas = \Phpcmf\Service::L('Input')->post('data');
	}

    public function edit() {

        !$this->cid && $this->_msg(0, dr_lang('缺少cid参数'));

        $id = intval(\Phpcmf\Service::L('Input')->get('id'));
		$xigai = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR.'_form_hdbm')->where('id', $id)->get()->getRowArray();
		

        // 是否有验证码
        $this->is_post_code = \Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'code', $this->member);
        
        list($tpl, $data) = $this->_Post($id);
		
		//dr_debug('uid', $xigai['uid'].$this->uid);
		//$row = \Phpcmf\Service::M()->db->table('module_form')->where('module', 'quan')->where('table', 'son')->get()->getRowArray();
		//$row = dr_string2array($row['setting']);
		//dr_debug('uid', $this->index['xiugai']);
		//var_dump($this->index);
		if ($this->index['xiugai'] == 0) {
			if ($this->index['uid'] == $this->uid) {
				// 添加二次开发
			} else {
				$this->_msg(0, dr_lang('已经关闭修改'));
			}
		} else {
			if ($xigai['uid'] == $this->uid) {
				// 添加二次开发
			} else {
				if ($this->index['uid'] == $this->uid) {
					//// 添加二次开发
				} else {
					$this->_msg(0, dr_lang('这个不是您的帖子，无法修改'));
				}
				
			}
		}
		
		

        !$data && $this->_msg(0, dr_lang('数据不存在: '.$id));
        $this->cid != $data['cid'] && $this->_msg(0, dr_lang('cid不匹配'));

        \Phpcmf\Service::V()->assign([
            'form' =>  dr_form_hidden(),
            'is_post_code' => $this->is_post_code,
        ]);
        \Phpcmf\Service::V()->display($tpl);
    }

    public function order_edit() {
        $this->_Display_Order(
            intval(\Phpcmf\Service::L('Input')->get('id')),
            intval(\Phpcmf\Service::L('Input')->get('value'))
        );
    }

    public function del() {
        $this->_Del(
            \Phpcmf\Service::L('Input')->get_post_ids(),
            null,
            function ($rows) {
                // 对应删除提醒
                foreach ($rows as $t) {
                    \Phpcmf\Service::M('member')->delete_admin_notice(MOD_DIR.'/'.$this->form['table'].'_verify/edit:cid/'.$t['cid'].'/id/'.$t['id'], SITE_ID);// clear
                    \Phpcmf\Service::L('cache')->clear('module_'.MOD_DIR.'_from_'.$this->form['table'].'_show_id_'.$t['id']);
                }
            },
            \Phpcmf\Service::M()->dbprefix($this->init['table'])
        );
    }
	//奖励
	public function jiangli() {
		if (IS_AJAX) {
			$quan = $_GET['quan'];
			$qiandao = 1;
			//奖励
			//$jianglis = \Phpcmf\Service::M()->db->table('module_form')->where('module', MOD_DIR)->where('table', $this->module['name'])->get()->getRowArray();
			$jianglis = \Phpcmf\Service::M()->db->table('module_form')->where('module', MOD_DIR)->where('table', $this->form['table'])->get()->getRowArray();
			$jiangli = dr_string2array($jianglis['setting']);
			
			$yuedujiangli = [
				'jingyan' => intval($jiangli['yueduexp']),
				'jifen' => intval($jiangli['yueduscore']),
				'yue' => intval($jiangli['yuedumoney']),
				'cishu' => intval($jiangli['yueducishu']),
				'miaoshu' => intval($jiangli['yuedujimiao']),
			];
			$urls = '/index.php?s='.MOD_DIR.'&c=son&id='.$quan;
			
			//
			
			// 增加经验
			\Phpcmf\Service::M('member')->add_experience($this->uid, $yuedujiangli['jifen'], dr_lang('阅读奖励'), $url = $urls, $mark = $quan, $count = 100, $tieid = $quan);
			// 增加积分
			\Phpcmf\Service::M('member')->add_score($this->uid, $yuedujiangli['jifen'], dr_lang('阅读奖励'), $url = $urls, $mark = $quan, $count = 100, $tieid = $quan);
			$this->_json(0, dr_lang('阅读奖励'), $qiandao);
		}
		$this->_json(0, dr_lang('请求错误'));
	}


    // 隐藏或者启用
    public function vip_edit() {

        $id = (int)\Phpcmf\Service::L('Input')->get('id');
        $row =\Phpcmf\Service::M()->table($this->init['table'])->get($id);
        !$row && $this->_json(0, dr_lang('数据不存在'));

        $v = $row['vip'] ? 0 : 1;
        \Phpcmf\Service::M()->table($this->init['table'])->update($id, ['vip' => $v]);

        exit($this->_json(1, dr_lang($v ? '已设置收费' : '已设置免费'), ['value' => $v]));
    }

    // 分类操作
    public function son_add() {

        if (IS_POST) {
            $name = dr_safe_replace($_POST['name']);
			$quanxian = dr_safe_replace($_POST['quanxian']);
			$yanse = dr_safe_replace($_POST['yanse']);
			$paixu = dr_safe_replace($_POST['paixu']);
			$moren = dr_safe_replace($_POST['moren']);
            if (!$name) {
                $this->_json(0, dr_lang('名称没有填写'));
            }
			$name = [
				'id' => '',
				'name' => $name,
				'quanxian' => $quanxian,
				'yanse' => $yanse,
				'paixu' => $paixu,
				'moren' => $moren,
			];
            $rt = \Phpcmf\Service::M('content', 'quan')->add_son_type($this->index, $name);
            if (!$rt['code']) {
                $this->_json(0, $rt['msg']);
            }
            $this->_json(1, dr_lang('操作成功'));
        }

        \Phpcmf\Service::V()->assign([
            'value' => '',
        ]);
        \Phpcmf\Service::V()->display('son_name.html');exit;
    }

    public function son_edit() {

        $tid = intval($_GET['tid']);
        $stype = \Phpcmf\Service::M('content', 'quan')->son_type($this->index['son_type']);

        if (IS_POST) {
            $name = dr_safe_replace($_POST['name']);
			$quanxian = dr_safe_replace($_POST['quanxian']);
			$yanse = dr_safe_replace($_POST['yanse']);
			$paixu = dr_safe_replace($_POST['paixu']);
			$moren = dr_safe_replace($_POST['moren']);
            !$name && $this->_json(0, dr_lang('名称没有填写'));
			$stype[$tid] = [
				'id' => $tid,
				'name' => $name,
				'quanxian' => $quanxian,
				'yanse' => $yanse,
				'paixu' => $paixu,
				'moren' => $moren,
			];
			//$stype[$quanxian] = $quanxian;
            \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR))->update($this->cid, [
                'son_type' => dr_array2string($stype)
            ]);
            $this->_json(1, dr_lang('操作成功'));
        }

        \Phpcmf\Service::V()->assign([
            'value' => $stype[$tid],
        ]);
        \Phpcmf\Service::V()->display('son_name.html');exit;

    }

    public function son_del() {

        $tid = intval($_GET['tid']);
        $stype = \Phpcmf\Service::M('content', 'quan')->son_type($this->index['son_type']);
        !$stype[$tid] && $this->_json(0, dr_lang('分类不存在'));
        unset($stype[$tid]);
        \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR))->where('uid', $this->uid)->update($this->cid, [
            'son_type' => dr_array2string($stype)
        ]);

        $data = \Phpcmf\Service::M()->table($this->init['table'])->where('uid', $this->uid)->where('cid', $this->cid)->where('tid', $tid)->getAll();
        if ($data) {
            foreach ($data as $t) {
                $_POST['ids'][] = $t['id'];
            }
            $this->del();
        }

        $this->_json(1, dr_lang('操作成功'));

    }
	// 添加bug
	public function son_add_bug() {
		/*if ($this->index['uid'] != $this->uid) {
			$this->_json(0, dr_lang('没有权限添加'.$this->index['uid']));
		}*/
		
		$quanid = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR)->where('id', $_GET['indexid'])->get()->getRowArray();
		if ($quanid['uid'] != $this->uid) {
			$this->_json(0, dr_lang('没有权限添加'.$quanid['uid']));
		}
	
	    if (IS_POST) {
	        $name = dr_safe_replace($_POST['name']);
			$quanxian = dr_safe_replace($_POST['quanxian']);
			$yanse = dr_safe_replace($_POST['yanse']);
			$paixu = dr_safe_replace($_POST['paixu']);
			$moren = dr_safe_replace($_POST['moren']);
	        if (!$name) {
	            $this->_json(0, dr_lang('名称没有填写'));
	        }
			 
			
			$name = [
				'id' => '',
				'name' => $name,
				'quanxian' => $quanxian,
				'yanse' => $yanse,
				'paixu' => $paixu,
				'moren' => $moren,
				'shijian' => SYS_TIME,
				'uid' => $this->uid,
			];
			$bugshuju = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR.'_form_son')->where('id', $_GET['id'])->get()->getRowArray();
			
	        $rt = \Phpcmf\Service::M('content', MOD_DIR)->add_son_bug($bugshuju, $name);
	        if (!$rt['code']) {
	            $this->_json(0, $rt['msg']);
	        }
	        $this->_json(1, dr_lang('操作成功'));
	    }
	
	    \Phpcmf\Service::V()->assign([
	        'value' => '',
	    ]);
	    \Phpcmf\Service::V()->display('son_bug.html');exit;
	}
	public function son_edit_bug() {
		$quanid = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR)->where('id', $_GET['indexid'])->get()->getRowArray();
		if ($quanid['uid'] != $this->uid) {
			$this->_json(0, dr_lang('没有权限修改'.$quanid['uid']));
		}
	
	    $tid = intval($_GET['tid']);
		$bugshuju = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR.'_form_son')->where('id', $_GET['id'])->get()->getRowArray();
	    $stype = \Phpcmf\Service::M('content', MOD_DIR)->son_bug($bugshuju['bugtitle']);
		
	
	    if (IS_POST) {
	        $name = dr_safe_replace($_POST['name']);
			$quanxian = dr_safe_replace($_POST['quanxian']);
			$yanse = dr_safe_replace($_POST['yanse']);
			$paixu = dr_safe_replace($_POST['paixu']);
			$moren = dr_safe_replace($_POST['moren']);
	        !$name && $this->_json(0, dr_lang('名称没有填写'));
			$stype[$tid] = [
				'id' => $tid,
				'name' => $name,
				'quanxian' => $quanxian,
				'yanse' => $yanse,
				'paixu' => $paixu,
				'moren' => $moren,
				'shijian' => SYS_TIME,
				//'uid' => $this->uid,
			];
			//$stype[$quanxian] = $quanxian;
	        \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son')->update($_GET['id'], [
	            'bugtitle' => dr_array2string($stype)
	        ]);
	        $this->_json(1, dr_lang('操作成功'));
	    }
	
	    \Phpcmf\Service::V()->assign([
	        'value' => $stype[$tid],
	    ]);
	    \Phpcmf\Service::V()->display('son_bug.html');exit;
	
	}
	public function son_del_bug() {
		$quanid = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR)->where('id', $_GET['indexid'])->get()->getRowArray();
		if ($quanid['uid'] != $this->uid) {
			$this->_json(0, dr_lang('没有权限删除'.$quanid['uid']));
		}
	
	    $tid = intval($_GET['tid']);
		
		$bugshuju = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR.'_form_son')->where('id', $_GET['id'])->get()->getRowArray();
		$stype = \Phpcmf\Service::M('content', MOD_DIR)->son_bug($bugshuju['bugtitle']);
	    !$stype[$tid] && $this->_json(0, dr_lang('分类不存在'));
	    unset($stype[$tid]);
	    \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR).'_form_son')->update($_GET['id'], [
	        'bugtitle' => dr_array2string($stype)
	    ]);
	
	    $this->_json(1, dr_lang('操作成功'));
	
	}

    /**
     * 获取内容
     * $id      内容id,新增为0
     * */
    protected function _Data($id = 0) {

        $row = $this->content_model->get_form_row($id, $this->form['table']);
        if (!$row) {
            return [];
        }

        return $row;
    }

    // 格式化保存数据 保存之前
    protected function _Format_Data($id, $data, $old) {

        // 验证父数据
		$tid = $_GET['tid'];
		//获取
		$huodongid = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_form_son')->where('id', $tid)->get()->getRowArray();
		if($huodongid['hdfs'] == 4) {
			$feiyong = $huodongid['hdfy'];
		} else {
			$feiyong = 0;
		}
		if($huodongid['hdfs'] == 5) {
			$huodongjifen = $huodongid['huodongjifen'];
		} else {
			$huodongjifen = 0;
		}
		//dr_debug('$huodongid', $huodongid);
		//if($yicanjia) {
		//标题
		if($huodongid['title']) {
			$titles = $huodongid['title'];
		} else {
			$titles = dr_strcut(dr_clearhtml($huodongid['neirong']), 30);
		}
        !$this->index && $this->_json(0, dr_lang('关联内容不存在'));
		dr_debug('data', $data);
		if (!$id) {
			if ($this->uid) {
				
				// 判断日发布量
				$day_posts = \Phpcmf\Service::M()->db->table('module_form')->where('module', MOD_DIR)->where('table', 'son')->get()->getRowArray();
				$day_post = dr_string2array($day_posts['setting']);
				 
				if ($day_post['day_post'] && \Phpcmf\Service::M()->db
						->table($this->init['table'])
						->where('uid', $this->uid)
						->where('DATEDIFF(from_unixtime(inputtime),now())=0')
						->countAllResults() >= $day_post['day_post']) {
					$this->_json(0, dr_lang('每天发布数量不能超过%s个', $day_post['day_post']));
				}
			
				// 判断发布总量
				if ($total_post['total_post'] && \Phpcmf\Service::M()->db
						->table($this->init['table'])
						->where('uid', $this->uid)
						->countAllResults() >= $total_post['total_post']) {
					$this->_json(0, dr_lang('发布数量不能超过%s个', $total_post['total_post']));
				}
				
			}
		}

        // 默认数据
		// 判断老数据
		if ($old['uid']) {
			//哪位用户的数据 等于本人那么id为$this->member['uid']
			if ($this->$old['uid'] == (int)$this->member['uid']){
				$data[0]['uid'] = (int)$this->member['uid'];
				$data[1]['author'] = $old['author'];
			} else {
				$data[0]['uid'] = (int)$old['uid'];
				$data[1]['author'] = $old['author'];
			}
		} else {
			$data[0]['uid'] = $data[1]['uid'] = (int)$this->member['uid'];
			$data[1]['author'] = $this->member['username'] ? $this->member['username'] : 'guest';
		}
        //$data[0]['uid'] = $data[1]['uid'] = (int)$this->member['uid'];
        //$data[1]['author'] = $this->member['username'] ? $this->member['username'] : 'guest';
		
        $data[1]['cid'] = $data[0]['cid'] =  $this->cid;
		$data[1]['tid'] = $tid;
        $data[1]['catid'] = $data[0]['catid'] = (int)$this->index['catid'];
		$data[1]['title'] = $titles;
		$data[1]['feiyong'] = $feiyong;
		$data[1]['jifen'] = $huodongjifen;
		$data[1]['bmzt'] = 0;
		$data[1]['fufeijifen'] = 0;
		$data[1]['shenhe'] = (int)$huodongid['baominghedui'];
        $data[1]['inputip'] = \Phpcmf\Service::L('Input')->ip_info();
        //$data[1]['inputtime'] = SYS_TIME;
		$m = \Phpcmf\Service::L('Input')->get('m');
		if ($m == 'edit') {
			$data[1]['updatetime'] = SYS_TIME;
		} else {
			$data[1]['inputtime'] = SYS_TIME;
		}

        // 审核状态
        $data[1]['status'] = \Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'verify', $this->member) ? 0 : 1;

        if (!$id) {
            $data[1]['tableid'] = 0;
            $data[1]['displayorder'] = 0;
        }

        return $data;
    }

    /**
     * 保存内容
     * $id      内容id,新增为0
     * $data    提交内容数组,留空为自动获取
     * $func    格式化提交的数据
     * */
    protected function _Save($id = 0, $data = [], $old = [], $func = null, $func2 = null) {

        return parent::_Save($id, $data, $old, null,
            function ($id, $data, $old) {
				$tid = $_GET['tid'];
                //审核通知
                if ($data[1]['status']) {
                    // 增减金币
                    $score = \Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'score', $this->member);
                    $score && \Phpcmf\Service::M('member')->add_score($this->member['uid'], $score, dr_lang('%s: %s发布', MODULE_NAME, $this->form['name']), $this->index['curl']);
                    // 增减经验
                    $exp = \Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'exp', $this->member);
                    $exp && \Phpcmf\Service::M('member')->add_experience($this->member['uid'], $exp, dr_lang('%s: %s发布', MODULE_NAME, $this->form['name']), $this->index['curl']);
                    // clear
                    \Phpcmf\Service::L('cache')->clear('module_'.MOD_DIR.'_from_'.$this->form['table'].'_show_id_'.$id);
					//更新统计
					$rt = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR.'_form_hdbm')->where('tid', $tid)->countAllResults();
					\Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR.'_form_son')->where('id', $tid)->update([
					    'huodongshuliang' => $rt,
					]);
					\Phpcmf\Service::M()->db->table(SITE_ID.'_'.MOD_DIR)->where('id', $tid)->update([
					    'hdbm_total' => $rt,
					]);
                    // 保存之后的更新total字段
                    $this->content_model->update_form_total( $this->cid, $this->form['table']);
                } else {
                    \Phpcmf\Service::M('member')->admin_notice(SITE_ID, 'content', $this->member, dr_lang('%s: %s提交内容审核', MODULE_NAME, $this->form['name']), MOD_DIR.'/'.$this->form['table'].'_verify/edit:cid/'. $this->cid.'/id/'.$id, SITE_ID);
                }
            }
        );
    }
}
